package com.yx.cloud.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yx.cloud.R;

/**
 * 通用确认对话框
 */
public class CommonConfirmDialog extends Dialog {

    private Context context;
    private String message;
    private ConfirmCallback confirmCallback;
    private int viewId = -1;

    public CommonConfirmDialog(Context context, String message,int viewId, ConfirmCallback confirmCallback) {
        super(context, R.style.dialog);
        this.context = context;
        this.message = message;
        this.confirmCallback = confirmCallback;
        this.viewId = viewId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(viewId);

        TextView tv_message = findViewById(R.id.tv_centered_dialogTitle);
        TextView btn_confirm = findViewById(R.id.btn_centered_Confirm);
        TextView btn_cancel = findViewById(R.id.btn_centered_Cancel);
        tv_message.setText(message);


        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                if (confirmCallback != null) {
                    //如果confirm返回false, 不取消对话框
                    if(confirmCallback.onConfirm()){
                        dismiss();
                    }
                } else {
                    dismiss();
                }
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                if (confirmCallback != null) {
                    if(confirmCallback.onCancel()){
                        dismiss();
                    }
                }
                dismiss();
            }
        });
        setCanceledOnTouchOutside(false);
    }

    public interface ConfirmCallback {
        boolean onConfirm();

        boolean onCancel();
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static final class Builder {
        private Context context;
        private String message;
        private ConfirmCallback confirmCallback;
        private int viewId = -1;

        private Builder() {
        }

        public Builder context(Context context) {
            this.context = context;
            return this;
        }

        public Builder message(String message) {
            this.message = message;
            return this;
        }

        public Builder viewId(int viewId) {
            this.viewId = viewId;
            return this;
        }

        public Builder confirmCallback(ConfirmCallback confirmCallback) {
            this.confirmCallback = confirmCallback;
            return this;
        }

        public CommonConfirmDialog build() {
            if (context == null) {
                throw new IllegalArgumentException("context should not be null");
            }
            CommonConfirmDialog commonConfirmDialog = new CommonConfirmDialog(context, message, viewId,confirmCallback);
            return commonConfirmDialog;
        }
    }

}
